module game{
    /**
     * 格子代理
     */
    export class McCandy extends eui.Component{
        public voCandy:CandyVO;

        public txt_value:eui.Label;
        private _count:number;
        private _candy:egret.Shape;

        private _left:egret.Shape;
        private _right:egret.Shape;
        private _up:egret.Shape;
        private _down:egret.Shape;
        constructor(){
            super();
            this.width = 110;
            this.height =110;

            this._left = new egret.Shape();
            this.addChild(this._left);
            this._right = new egret.Shape();
            this.addChild(this._right);
            this._up = new egret.Shape();
            this.addChild(this._up);
            this._down = new egret.Shape();
            this.addChild(this._down);

            this._candy = new egret.Shape();
            this.addChild(this._candy);

            this.txt_value = new eui.Label();
            this.txt_value.fontFamily = "SimHei";
            this.txt_value.textColor = 0x000000;
            this.txt_value.size = 64;
            this.txt_value.bold = true;
            this.txt_value.width = 110;
            this.txt_value.height = 110;
            this.txt_value.textAlign = "center";
            this.txt_value.y = this.height - 64 >> 1;
            this.addChild(this.txt_value);

             this.anchorOffsetX = this.width >> 1;
             this.$anchorOffsetY = this.height >> 1;

        }

        public init(vo:CandyVO){
            this.voCandy = vo;
            this.setCandyValue(vo);
            this.scaleX = 1;
            this.scaleY = 1;
            this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
        }

        public dispose(){
             this.reset();
            this.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onClick,this);
            if(this.parent){
                this.parent.removeChild(this);
            }
        }

        public disposeMin(){
            egret.Tween.removeTweens(this);
            let t = egret.Tween.get(this).to({
                rotation:720,scaleX:0,scaleY:0
            },500)

            t.call(this.dispose,this);

        }

        private count:number = 0;
        public gameOver(){
            this.count = 0;
            this.tween_left();
            
        }

        private gaptime = 50;
        private ox = 0;
        private tween_left(){
            this.count++;
            this.ox = this.voCandy.x;
            if(this.count*this.gaptime == 1000){
                egret.Tween.removeTweens(this);
                egret.Tween.get(this).to({x:this.ox},this.gaptime);
                return;
            }
            egret.Tween.removeTweens(this);
            egret.Tween.get(this).to({x:this.ox-4},this.gaptime).call(this.tween_right,this);
        }

        private tween_right(){
            this.count++;
            if(this.count *this.gaptime == 1000){
                egret.Tween.removeTweens(this);
                egret.Tween.get(this).to({x:this.ox},this.gaptime);
                return;
            }
            egret.Tween.removeTweens(this);
            egret.Tween.get(this).to({x:this.ox+4},this.gaptime).call(this.tween_left,this);
        }


        private _color = [0xffffff,0XEEC024,0xAFD04E , 0xD47F49 ,0xCF4B49 ,0xD34997, 0x9C3FCE ,0x733BC6, 0x3340BE, 0x0C7EC4, 0x2DAFBA, 0x6FF1E1, 0xCBF4EF];
        
        private preColor:number;
        public setCandyValue(vo:CandyVO){
            let color ;
            if(vo.type == gameData.curmax) color = this._color[0];
            else color = this._color[vo.type%12+1];

            if(color == this.preColor){
                this.txt_value.text = vo.type+"";
                return;
            }

            let g =this._candy.graphics;
            g.clear();
            g.beginFill(color);
            g.drawRoundRect(0,0,vo.width,vo.height,15);
            g.endFill();

            // //左
            g = this._left.graphics;
            g.clear();
            g.beginFill(color);
            g.drawRect(-8,0,15,vo.height);
            g.endFill();

            // //右
            g = this._right.graphics;
            g.clear();
            g.beginFill(color);
            g.drawRect(vo.width+8 -15,0,15,vo.height);
            g.endFill();

            // 上
            g = this._up.graphics;
            g.clear();
            g.beginFill(color);
            g.drawRect(0,-8,vo.width,15);
            g.endFill();

            //下
            g = this._down.graphics;
            g.clear();
            g.beginFill(color);
            g.drawRect(0,vo.height -15+8,vo.width,15);
            g.endFill();

            this.txt_value.text = vo.type+"";
            this.preColor = color; 

            this.reset();
        }

        public reset(){
            this._down.visible = false;
            this._up.visible = false;
            this._left.visible = false;
            this._right.visible = false;
            this._edgeList = [];
        }
        public showEdge():void{
            for (let type of this._edgeList){
                this.setEdge(type);
            }
        }
        private _edgeList:any[] =  [];
        public setEdge(type){
            switch(type){
                case EdgeType.Left:
                    this._left.visible = true;
                break;
                case EdgeType.Right:
                    this._right.visible = true;
                break;
                case EdgeType.Up:
                    this._up.visible = true;
                break;
                case EdgeType.Down:
                    this._down.visible = true;
                break;
                
            }
            this._edgeList.push(type);
            
        }
        public ani(time = 0):void{
            for (let type of this._edgeList){
                this.aniType(type);
            }
        }
        private aniType(type):void{
            switch(type){
                case EdgeType.Left:
                    this.x -= this.width;
                    this.scaleX = 0.5;
                break;
                case EdgeType.Right:
                    this.scaleX = 0.5;
                    
                break;
                case EdgeType.Up:
                    this.scaleY = 0.5;
                break;
                case EdgeType.Down:
                    this.y -= this.height;
                    this.scaleY = 0.5;
                    
                    // this._down.visible = true;
                break;
                
            }
        }
        private onClick(){
            App.ControllerManager.applyFunc(ControllerConst.Core,EventConst.Remove_Candy,this);
        }
        
    }
}